package com.meitianhui.convenitenbuy.widget.wheelview;

import android.content.Context;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.module.mine.model.Region;
import com.meitianhui.convenitenbuy.widget.wheelview.adapter.ArrayWheelAdapter;

import java.util.List;

/**
 * 地区选择控件
 * Created by liuwy on 2015/12/17.
 */
public class RegionPopWindow extends PopWindowWheelSelector implements OnWheelChangedListener {


    /**
     * 数据集合
     **/
//    private List<Region> list ;
//    private List<Region.Children> cityList;
//    private List<Region.Children.ChildClass> distristList;
    private Context context;
    private TextView tvRegion;
    private TextView tvCode;

    private String result;

    private OnViewClickListener mListener;

    public RegionPopWindow(Context context, List list, List cityList, List distristList, TextView tvRegion, TextView tvCode, OnViewClickListener listener) {
        super(context, list, cityList, distristList);
        this.list = list;
        this.cityList = cityList;
        this.distristList = distristList;
        this.context = context;
        this.tvRegion = tvRegion;
        this.tvCode = tvCode;
        this.mListener = listener;
        init();
    }


    public interface OnViewClickListener{
         void ConfirmClick();
    }


    @Override
    public void onDismiss(Region curItem, Region.Children childrenItem, Region.Children.ChildClass childClassItem, int curIndex) {
         if(curItem.getValue().equals(childrenItem.getValue())){
             result = childrenItem.getValue() + "," + childClassItem.getValue();
         }else {
        result = curItem.getValue() + "," + childrenItem.getValue() + "," + childClassItem.getValue();
         }
//        if(curItem.getId().equals(childrenItem.getId())){//如果父类地址ID == 子类地址ID则只取子类地址ID
//            tvCode.setText(childrenItem.getId() + "," + childClassItem.getId());
//        }else {
//            tvCode.setText(curItem.getId() + "," + childrenItem.getId() + "," + childClassItem.getId());
//        }
        tvCode.setText(childClassItem.getId());
        tvRegion.setText(result);


        //暴露回调接口
        mListener.ConfirmClick();
    }


    /**
     * 初始化
     */
    private void init() {
//      initWheelAdapter(context,list,cityList,distristList);

        mWheelView.setVisibleItems(7);
        mCityWheelView.setVisibleItems(7);
        mDistrictWheelView.setVisibleItems(7);
        setProvinceData();
        updateCities();
        updateAreas();
        mWheelView.addChangingListener(this);
        mCityWheelView.addChangingListener(this);
        mDistrictWheelView.addChangingListener(this);
    }

    @Override
    public String getItem(int index) {
        return null;
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mWheelView) {
            updateCities();
        } else if (wheel == mCityWheelView) {
            updateAreas();
        }
    }

    private void setProvinceData() {
        mProvinceAdapter = new ArrayWheelAdapter(context, list.toArray(new Object[list.size()]));
        mWheelView.setViewAdapter(mProvinceAdapter);
        mWheelView.setCurrentItem(0);
    }


    private void updateAreas() {
        int current = mCityWheelView.getCurrentItem();
        List<Region.Children.ChildClass> childclassList = cityList.get(current).getChildren();
        if (childclassList != null && childclassList.size() != 0) {
            distristList.clear();
            distristList.addAll(childclassList);
            mDistrictAdapter = new ArrayWheelAdapter(context, distristList.toArray(new Object[distristList.size()]));
        } else {
            mCityAdapter = new ArrayWheelAdapter(context, cityList.toArray(new Object[cityList.size()]));
        }

        mDistrictWheelView.setViewAdapter(mDistrictAdapter);
        mDistrictWheelView.setCurrentItem(0);
    }


    private void updateCities() {
        int current = mWheelView.getCurrentItem();
        List<Region.Children> childrenList = list.get(current).getChildren();
        cityList.clear();
        cityList.addAll(childrenList);
        mCityAdapter = new ArrayWheelAdapter(context, cityList.toArray(new Object[cityList.size()]));
        mCityWheelView.setViewAdapter(mCityAdapter);
        mCityWheelView.setCurrentItem(0);
        updateAreas();
    }


}
